#include <QCoreApplication>
#include <iostream>
#include <QDebug>
#include <QList>

QString getLongestCommonPrefix(QList<QString> strs);

int main(int argc, char *argv[])
{
    using namespace std;
    QCoreApplication a(argc, argv);
    QList<QString> inputStrs;
    QString commonStr = "1";
    inputStrs << "ab" << "abvx" << "aaaaa";       //输入字符串组
    commonStr = getLongestCommonPrefix(inputStrs);
    qDebug() << "Their Longest Common Prefix is"<< commonStr << endl;   //输出最长公共前缀
    return a.exec();
}

QString getLongestCommonPrefix(QList<QString> strs)
{
    using namespace std;
    QString commonStr = "";
    int i = 0;
    int j = 0;
    int flag = 1;
    int commonNum = 0;      //相同前缀长度
    int shortestSize = strs[0].size();
    for (i = 0; i < strs.size(); i++ ){   //计算最短字符串的字符数量
        if (shortestSize > strs[i].size())
                shortestSize = strs[i].size();
    }
    for (i = 0; i < shortestSize; i++){     //计算相同前缀长度
        for (j = 1; j < strs.size(); j++){
            if (strs[0][i] != strs[j][i]){
                flag = 0;
                break;
            }
        }
        if (flag == 1){
            commonNum++;    //验证完成，相同前缀长度加1
        }
        else break;
    }
    commonStr = strs[0].left(commonNum);
    return commonStr;
}
